package evaluation;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class RankingGenerator {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		System.out.println("START");
		
		String type = "training";
		
		File folder = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/SizeSortedList/" + type + "/");
		
		File output = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/SizeSortedList/" + type + ".ranking.txt");
		
		File prefix = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/QueryResults/" + type + "/");
		
		File[] files = folder.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				return pathname.getName().endsWith("clean");
			}
		});
		
		Map<String,List<Integer>> positions = new HashMap<String, List<Integer>>();
		
		Map<Long,Integer> diffTable = new HashMap<Long, Integer>();
		
		for (int i = 0; i < files.length; i++) {
			
			String name = files[i].getName().substring(0,files[i].getName().indexOf('.'));
			
			BufferedReader br = new BufferedReader(new FileReader(files[i]));
			
			String line;
			
			List<File> filesArray = new ArrayList<File>();
			
			while ((line = br.readLine()) != null){
				
				filesArray.add(new File(prefix,name + "/" + line));
				
			}
			
			br.close();
			
			int index = 1;
			
			int lastIndex = 1;
			
			if (filesArray.isEmpty())
				continue;
			
			File lastF = filesArray.get(0);
			
			for (File file : filesArray) {
				
				double diff = lastF.length() - file.length();
				
				diff = diff/(lastF.length()/2.0 + file.length()/2.0);
				
				long val = (long)(diff * 10000.0);
				
				if (val < 14){
					
					getPos(file.getName(), positions).add(lastIndex);
								
					
				} else {
					
					updateDiff(diffTable,val);
					
					lastF = file;
					
					getPos(file.getName(),positions).add(index);
					
					lastIndex = index;
					
				}
				
				index++;
				
			}
			
		}

		BufferedWriter bw = new BufferedWriter(new FileWriter(output));
		
		for (Entry<String, List<Integer>> entry : positions.entrySet()) {
			
			double value = calculateRankingValue(entry.getValue());
			
			bw.write(entry.getKey() + "," + value + "\n");
			
		}
		
		bw.close();
		
		for (Entry<Long,Integer> entry : diffTable.entrySet()) {
			
			System.out.println(entry.getKey() + ",," + entry.getValue());
			
		}
	}

	private static void updateDiff(Map<Long, Integer> diffTable, long differ) {
		
		Integer freq = diffTable.get(differ);
		
		if (freq == null){
			
			freq = 0;
			
		}
		
		freq += 1;
		
		diffTable.put(differ, freq);
		
	}

	private static double calculateRankingValue(List<Integer> value) {
		
		double acc = 0.0;
		
		for (Integer integer : value) {
			
			acc += integer;
			
		}
		
		return acc/value.size();
	}

	private static List<Integer> getPos(String line,
			Map<String, List<Integer>> positions) {
		
		List<Integer> ret = positions.get(line);
		
		if (ret == null){
			ret = new ArrayList<Integer>();
			positions.put(line, ret);
		}
		
		return ret;
	}

}
